<?php
/*
 * 从数据库生成定义文件
 * @since 1.0 2014-9-1 by sutroon
 */
require 'Inc/incDbHead.php';
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="/Public/Script/jquery-1.11.1.min.js"></script>
        <style type="text/css">
            body { font-size:12px;}
            table{width:100%; border: solid 1px #CCC; border-left:none; border-bottom: none;}
            th:first-child{width:30px;}
            .th-field {width:500px;}
            th, td { padding: 5px; border:solid 1px #CCC; border-right: none; border-top: none;}
            tr.tr-alt { background-color: #efefef;}
            a { text-decoration: none;}
            fieldset{ margin-bottom:10px; width:960px;}
            fieldset textarea { width:100%; height:600px;}
        </style>
    </head>
    <body>
        <?php
        // 显示数据库登录表单
        if (!$conn_info) {
            echoDbLoginForm();
        }
        // 全局菜单
        include 'Inc/incMenu.php';

        $arrTableCols = array('TABLE_NAME', 'ENGINE', 'TABLE_COLLATION', 'TABLE_COMMENT');
        $arrFieldCols = array('Field', 'Type', 'Null', 'Key', 'Default', 'Extra', 'Comment');
        $mysql_charset = array('big5' => 'big5_chinese_ci', 'dec8' => 'dec8_swedish_ci', 'cp850' => 'cp850_general_ci', 'hp8' => 'hp8_english_ci', 'koi8r' => 'koi8r_general_ci', 'latin1' => 'latin1_swedish_ci', 'latin2' => 'latin2_general_ci', 'swe7' => 'swe7_swedish_ci', 'ascii' => 'ascii_general_ci', 'ujis' => 'ujis_japanese_ci', 'sjis' => 'sjis_japanese_ci', 'hebrew' => 'hebrew_general_ci', 'tis620' => 'tis620_thai_ci', 'euckr' => 'euckr_korean_ci', 'koi8u' => 'koi8u_general_ci', 'gb2312' => 'gb2312_chinese_ci', 'greek' => 'greek_general_ci', 'cp1250' => 'cp1250_general_ci', 'gbk' => 'gbk_chinese_ci', 'latin5' => 'latin5_turkish_ci', 'armscii8' => 'armscii8_general_ci', 'utf8' => 'utf8_general_ci', 'ucs2' => 'ucs2_general_ci', 'cp866' => 'cp866_general_ci', 'keybcs2' => 'keybcs2_general_ci', 'macce' => 'macce_general_ci', 'macroman' => 'macroman_general_ci', 'cp852 ' => 'cp852_general_ci', 'latin7' => 'latin7_general_ci', 'cp1251' => 'cp1251_general_ci', 'cp1256' => 'cp1256_general_ci', 'cp1257' => 'cp1257_general_ci', 'binary' => 'binary', 'geostd8' => 'geostd8_general_ci', 'cp932' => 'cp932_japanese_ci', 'eucjpms' => 'eucjpms_japanese_ci');

        /*
         * ==Field_ACD (AutoCodingData)==
         * 'Ordinal'=>999, // 排列次序
         * 'Title'=>'', // 字段标题
         * 'FormInputType'=>'', // 控件类型,text=文本输入框&hidden=隐藏域&file=文件域&textarea=多行文本域&editor=编辑器&select=下拉框&radio=单选组&checkbox=复选框&password=密码框&timepicker=时间选择器&datepicker=日期选择器
         * 'FormRequire'=>FALSE, // 是否表单必填项
         * 'FormValueVar'=>'',  // 表单值变量,默认是$data,如:$data.field,如果不是以$开头则视为固定值
         * 'FormJSONSource'=>'', // 表单下拉框JSON数据源,如:?s=Model/get_json.html
         * 'FormValueRange'=>'',  // 表单下拉框或选项组的值范围,如:!0=正常&1=禁用 (值的前面加符号[!]表示选中)
         * 'ListValueFunction'=>'', // 列表值函数,如：get_state_text=user,###则会自动格式化为{$vo.state|get_state_text=user,###}
         * 'ParamAddParam'=>TRUE, // 参数-是否$params->addParam('Field','require','xxx未填写!');
         * 'ParamAddOptional'=>TRUE, // 参数-是否$params->addOptional('Field','number','xxx无效!');
         * 'ParamAddData'=>TRUE // 参数-是否$params->addData('Field');
         * 'ParamConfirm'=>TRUE, // 参数-是否$params->confirm_param(I('Field'), I('reField'), '两次xxx输入不一致!');
         */

        echo '<fieldset><legend>Table or List</legend><textarea>';
        echo '<?php'.$ln.'/*' . $ln . '* DefinedFile - '.$database . $ln . '* 数据表字段定义文件' . $ln . '* @since 1.0 ' . date('Y-m-d') . ' by sutroon' . $ln . '*/' . $ln . $ln;
        echo '$DATABASE_SET = array(' . $ln;
        $result = mysql_query("select TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, TABLE_ROWS, DATA_LENGTH, AUTO_INCREMENT, CREATE_TIME, `TABLE_COLLATION`, TABLE_COMMENT  from  information_schema.TABLES where TABLE_SCHEMA='$database';") or die(mysql_error());
        while ($rTable = mysql_fetch_array($result, MYSQL_ASSOC)) {
            echo $tab . 'array(' . $ln;
            foreach ($rTable as $keyTable => $valTable) {
                if (in_array($keyTable, $arrTableCols)) {
                    echo $tab . $tab . "'$keyTable' => '" . (in_array($valTable, $mysql_charset) ? array_search($valTable, $mysql_charset) : $valTable) . "'," . $ln;
                }
            }
            $result2 = mysql_query('SHOW FULL FIELDS FROM ' . $rTable['TABLE_NAME'] . ';');
            if ($result2 === false) {
                continue;
            }
            echo $tab . $tab . "'TABLE_FIELDS'" . ' => array(' . $ln;
            while ($rField = mysql_fetch_array($result2, MYSQL_ASSOC)) {
                echo $tab . $tab . $tab . 'array(';
                foreach ($rField as $keyField => $valField) {
                    if (in_array($keyField, $arrFieldCols)) {
                        echo "'$keyField' => '$valField', ";
                    }
                }
                echo "'AutoCoding' => array('Ordinal'=>999, 'Title'=>'" . (strpos($rField['Comment'], '(') ? substr($rField['Comment'], 0, strpos($rField['Comment'], '(')) : ($rField['Comment'] ? $rField['Comment'] : $rField['Field'])) . "'" . get_control_type($rField) . ')';
                echo '),' . $ln;
            }
            echo $tab . $tab . '),' . $ln;
            echo $tab . '),' . $ln;
        }
        echo ');';
        echo '</textarea></fieldset>';
        // 释放资源
        mysql_free_result($result);
        unset($db);

        /**
         * 根据字段名称获取控件类型
         * @param array $row
         * @return string
         * @since 1.0 2014-9-1 by sutroon
         */
        function get_control_type($row) {
            $field = $row['Field'];
            $type = 'text';
            $jsonSource = '';
            $valueRange = '';
            $listValueFunc = '';
            // text=文本输入框&hidden=隐藏域&file=文件域&textarea=多行文本域&editor=编辑器&select=下拉框&radio=单选组&checkbox=复选框&password=密码框&timepicker=时间选择器&datepicker=日期选择器
            if ($field == 'id') {
                $type = 'hidden';
            } else if ($field == 'description') {
                $type = 'editor';
            } else {
                if (sofunc_end_with($field, 'state', true)) {
                    $type = 'radio';
                    $listValueFunc = 'get_state_text=###';
                    if (strpos($row['Comment'], '(')) {
                        $valueRange = substr($row['Comment'], strpos($row['Comment'], '(') + 1);
                        $valueRange = substr($valueRange, 0, strpos($valueRange, ')'));
                    } else {
                        $valueRange = '!=正常&2=待审核&4=关闭';
                    }
                } else if (sofunc_end_with($field, 'id', true)) {
                    $type = 'select';
                    $jsonSource = '?s=M/A.html';
                } else if (sofunc_end_with($field, 'time')) {
                    $type = 'timepicker';
                } else if (sofunc_end_with($field, 'date')) {
                    $type = 'datepicker';
                } else if (sofunc_end_with($field, 'password')) {
                    $type = 'password';
                } else if (sofunc_end_with($field, 'content') || sofunc_end_with($field, 'text')) {
                    $type = 'textarea';
                } else if (stripos($field, 'image') !== false || stripos($field, 'thumb') !== false || stripos($field, 'picture') !== false || stripos($field, 'filepath') !== false || stripos($field, 'attachment') !== false) {
                    $type = 'file';
                }
            }
            return ", 'FormInputType' => '$type', 'FormRequire' => TRUE, 'FormValueVar' => '\$data', 'FormJSONSource' => '$jsonSource', 'FormValueRange' => '$valueRange', 'ListValueFunction' => '$listValueFunc', 'ParamAddParam' => TRUE, 'ParamAddOptional' => FALSE, 'ParamAddData' => TRUE";
        }
        ?>
        <script type="text/javascript">
            // fieldset标题点击收起或展开 2014-8-22 by sutroon
            $('legend').attr('title', '点击展开或收起').click(function() {
                $(this).next().toggle();
            })
        </script>
    </body>
</html>
